/*
 * PlayScenarioPanel.cpp
 *
 *  Created on: Oct 24, 2011
 *      Author: gonzalo
 */

#include "PlayScenarioPanel.h"

PlayScenarioPanel::PlayScenarioPanel(int xPos, int yPos, Widget* parent,SpaceBattleInterface* interface) :
		ScenarioPanel(xPos, yPos,parent) {
	this->owner = interface->getOwner();

	//Read the xml and build the elements
	this->addAsteroides(interface->getAsteroides());

	if (interface->getShip() != NULL){
		this->addPlayer(interface->getShip());
	}
}

void PlayScenarioPanel::addAsteroides(std::list<Asteroide*> asteroidesList){
	std::list<Asteroide*>::iterator asteroide;

	for (asteroide = asteroidesList.begin(); asteroide != asteroidesList.end();
			++asteroide) {
		this->asteroides.push_back(*asteroide);
		//Add it to the scenarioPanel as well
		add(*asteroide);
	}
}

void PlayScenarioPanel::addPlayer(ShipStatus* shipStatus){

	//There is bug here apparently
	if(shipStatus == NULL){
		Logger::log("PlayScenarioPanel","addPlayer","ShipStatus is null!");
		return;
	}

	//Verifico que esta.
	bool found =false;

	Ship* jugador = NULL;
	std::list<Ship*>::iterator it_jugador;
	for (it_jugador = jugadores.begin(); it_jugador != jugadores.end();
			++it_jugador) {
		jugador = *it_jugador;
		if (jugador->getOwner() == shipStatus->getOwner()) {
			//TODO hacer algo... actualizar estado o algo
			found = true;
		}
	}

	//Si no lo encontre lo agrego a la pantalla
	if (!found) {
		addPlayer(shipStatus->getImagePath(),shipStatus->getVelRotacion(),shipStatus->getVelMax(),shipStatus->getPropultion(),shipStatus->getFriccion()
				,shipStatus->getDuracionBala(),
				shipStatus->getOwner(),shipStatus->getPosX(),shipStatus->getPosY());
	}

}

void PlayScenarioPanel::addPlayer(std::string imagen,float velRotacion,int velMaxima,int propulsion,
				int friccion,int duracionBala,std::string owner, int x, int y,int angle) {
	Ship* newShip = new Ship(imagen,velRotacion,velMaxima,propulsion,friccion,duracionBala,x, y, this, owner,angle);

	//TODO remover Parche
	if (jugadores.empty()) {
		newShip->duenoesinstancialocal = true;
	}
	this->jugadores.push_back(newShip);


	this->add(newShip);
}

void PlayScenarioPanel::resolve() {

	Ship* jugador = NULL;

	std::list<Bullet*>::iterator aux;
	std::list<Bullet*>::iterator it_bullet;

	//TODO GPV, aca esta la logica para borrar las balas que murieron por tiempo
	//el todo es para verificar que quede solo aca
	for (it_bullet = bullets.begin(); it_bullet != bullets.end(); ++it_bullet) {
		aux = (it_bullet);
		if (!(*aux)->isDrawable()) {
			//eraseBullet(aux);
		}
	}

	std::list<Ship*>::iterator it_jugador;
	for (it_jugador = jugadores.begin(); it_jugador != jugadores.end();
			++it_jugador) {
		jugador = *it_jugador;
//		jugador->resolve();
		if (jugador->isShooting()) {
			addBullet(jugador->getbulletX(), jugador->getbulletY(), jugador->getBulletAngle(),jugador->getBulletDuration());
		}
	}

	ScenarioPanel::resolve();
}

void PlayScenarioPanel::addBullet(int x, int y, int angle, int duracionBala) {
	Bullet* bullet = new Bullet(x, y, this, angle, duracionBala);
	bullets.push_back(bullet);

	add(bullet);
}

void PlayScenarioPanel::eraseBullet(std::list<Bullet*>::iterator bullet) {
	erase((*bullet));
	bullets.erase(bullet);
}


ShipStatus PlayScenarioPanel::getShipStatus() {
	return ((Ship*) this->getPlayerShip())->getShipStatus();
}

Ship* PlayScenarioPanel::getPlayerShip() {
	std::list<Ship*>::iterator it_Ship;

	Ship* tanque = NULL;
	std::list<Ship*>::iterator it_jugador;
	for (it_Ship = jugadores.begin(); it_Ship != jugadores.end(); ++it_Ship) {
		tanque = ((Ship*) *it_Ship);
		if (tanque->duenoesinstancialocal)
			return tanque;
	}

	return tanque;
}

PlayScenarioPanel::~PlayScenarioPanel() {

}
